/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.archive.portlet;

import java.io.IOException;

import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

import cn.com.chinasoa.archive.runtime.ArchiveRuntime;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;
import com.x4ee.soa.portal.framework.core.FrameworkConfig;

/**
 * 文档下载
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-8-3下午03:50:35
 */
public class ArchiveDownloadActionFilter extends BaseRequestFilter implements
		ActionFilter {

	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {
		HttpServletResponse portalResponse = FrameworkConfig.getPortalResponse(actionResponse);
		if(portalResponse!=null){
			Repository tRepository = ArchiveRuntime.getRepository(actionRequest);
    		if(tRepository != null) {
    			try {
    				portalResponse.setHeader("Cache-Control", "max-age=0; must-revalidate");	
    				String nodeName = actionRequest.getParameter(ArchiveUploadHelper.ARCHIVE_NODE_NAME);
    				if(nodeName!=null){
    			        Session session=null;
    					try {
    						session = tRepository.login(new SimpleCredentials(ArchiveUploadHelper.SEP,ArchiveUploadHelper.X4EE.toCharArray()));
    			            Node root = session.getRootNode();
    			            Node archiveNode = root.getNode(nodeName);
    			            if(archiveNode!=null){
    				            Property archiveNamePty = archiveNode.getProperty(ArchiveUploadHelper.ARCHIVE_NAME);
    				            Property archiveFilePty = archiveNode.getProperty(ArchiveUploadHelper.ARCHIVE_FILE);     				            
    				            String archiveName = archiveNamePty.getValue().getString();
    							portalResponse.setHeader("Content-Type","application/octet-stream;charset=gb2312");
    							portalResponse.setHeader("Cache-Control", "max-age=0; must-revalidate");
    							portalResponse.addHeader("Content-Disposition", "attachment; filename=" + archiveName);
    	    					IOUtils.copy(archiveFilePty.getStream(),portalResponse.getOutputStream());
    				        }
    					} catch (LoginException e) {
    						e.printStackTrace();
    					} catch (RepositoryException e) {
    						e.printStackTrace();
    					} finally {
    						if(session!=null)session.logout();
    			        }
    				}
    				portalResponse.flushBuffer();
    			} catch (Exception e) {
    				return ArchiveUploadHelper.ERROR;
    			}
    			return ArchiveUploadHelper.SUCCESS;
    		}else{
    			return ArchiveUploadHelper.ERROR;			
    		}  			
		}else{
			return ArchiveUploadHelper.ERROR;
		}
	}

}
